/*
 * Copyright 2011-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.mgmt.framework.spring;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import me.ijleex.mgmt.commons.util.Utils;
import me.ijleex.mgmt.framework.entity.EntitySupport;
import me.ijleex.mgmt.framework.page.Page;
import me.ijleex.mgmt.framework.page.util.PageCalcUtils;

/**
 * 控制器层入口
 *
 * @param <T> 模型
 * @author liym
 * @see org.springframework.stereotype.Controller
 * @since 2015-10-23 14:51:48
 * @since 2019-03-02 15:37 新建
 */
public class ControllerSupport<T extends EntitySupport> {

    /**
     * 日志
     */
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 设置分页参数
     *
     * <p>
     * <pre>
     * this.setPageParams(request, entity);
     * this.wrapPageData(this.service.selectList(entity), entity);
     * </pre>
     * </p>
     *
     * @param request {@linkplain HttpServletRequest HTTP 请求}
     * @param entity {@linkplain EntitySupport 实体类，用于设置分页参数}
     * @author liym
     * @version 2015-10-26 22:50:11 新建
     * @version 2015-10-27 14:53:33 分页时，请求参数一般有两种：pageNo、pageSize 或 offset、limit；
     * 如果是 offset、limit，可以直接用于SQL查询；如果是 pageNo、pageSize，则需要计算 offset、limit，方法为 {@link PageCalcUtils#calcOffset}
     * @see #wrapPageData(List, EntitySupport)
     * @see com.github.pagehelper.PageHelper#startPage(int, int, boolean)
     */
    protected void setPageParams(HttpServletRequest request, EntitySupport entity) {

        // Element-UI Pagination 使用 pageNo、pageSize 作为分页参数；
        // 如果前端分页插件使用的的分页参数为 offset、limit，请用如下的方法计算 pageNo、pageSize：

        int pageNo = entity.getPageNo();
        int pageSize = entity.getPageSize();
        if (pageNo <= 1 && pageSize < 1) {
            int offset = Utils.safeToInt(request.getParameter("offset"));
            int limit = Utils.safeToInt(request.getParameter("limit"));

            pageNo = PageCalcUtils.calcPageNo(offset, limit);
            pageSize = limit;

            entity.setPageNo(pageNo);
            entity.setPageSize(pageSize);
        }
        if (pageNo >= 1 && pageSize >= 1) {
            PageHelper.startPage(pageNo, pageSize, true);
        }
    }

    /**
     * 初始化页面数据
     * <p>
     * <pre>
     * this.setPageParams(request, entity);
     * this.wrapPageData(this.service.selectList(entity), entity);
     * </pre>
     * </p>
     *
     * @param rowsList 数据
     * @param entity {@linkplain EntitySupport#getTotalCount() 查询对象}，用于取得 <b>数据总条数</b>
     * @return 分页结果数据
     * @author liym
     * @version 2015-10-26 22:53:43
     * @see #setPageParams(HttpServletRequest, EntitySupport)
     * @see PageCalcUtils#calcPageCount
     * @see com.github.pagehelper.PageInfo
     */
    protected Page<T> wrapPageData(List<T> rowsList, EntitySupport entity) {
        long total = entity.getTotalCount();
        if (rowsList == null || rowsList.isEmpty()) {
            total = 0;
            rowsList = Collections.emptyList();
        } else {
            if (total == 0) {
                /// count = rowsList.size();
                PageInfo<T> pageInfo = new PageInfo<>(rowsList);
                total = pageInfo.getTotal();
                entity.setTotalCount(total);
            }
        }
        return new Page<>(total, rowsList);
    }

}
